package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.Groups;
import com.vne.skyemart.service.GroupsService;

@Controller
@SessionAttributes
@RequestMapping("/groups")
public class GroupsController {

	@Autowired
	private GroupsService groupsService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveGroups(@ModelAttribute(" groups") Groups groups,
			BindingResult result) {
		 groupsService.addGroups(groups);
		return new ModelAndView("redirect:/groups.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listGroups(Model model) {
		model.addAttribute("groups",  groupsService.listGroups());

		return "groupsList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addGroups(@ModelAttribute("groups") Groups groups,
			BindingResult result) {
		return new ModelAndView("addGroups");
	}

}